<?php
/**
 * @version 	$Id: fProfiler.class.php 82 2010-03-17 08:45:39Z mylifeisskidrow@gmail.com $
 * @author 		$Author: mylifeisskidrow $
 * @copyright 	Copyright (c) 2010 Luu Trong Hieu. All right reserved.
 * @license 	GNU/GPL see license.php
 */

defined('IN_FLYWHEEL') or die('Restricted Access');

/**
 * Profiler
 * Object xu dung danh dau cac hoat dong cua project
 * xu dung trong moi truong debug 
 * @package 	system
 * @subpackage 	log 
 *
 */

class fProfiler {	
	/**
	 * Buffer
	 *
	 * @var string
	 */
	private $_buffer;
	
	/**
	 * Start
	 * Thoi diem bat dau
	 * 
	 * @var int	 
	 */
	private $_start = 0;
	/**
	 * Instance;
	 *
	 * @var object Profiler
	 */	
	private static $_instance;
	
	private function __construct() 
	{
		$this->_start = $this->_getMicrotime();
		$this->_buffer = array();
	}
	
	/**
	 * Get Instance
	 *
	 * @return fProfiler
	 */
	public static function getInstance()
	{
		if (!isset(self::$_instance))
		{
			self::$_instance = new fProfiler();		
		}
		
		return self::$_instance;
	}
	
	/**
	 * Get Buffer
	 *
	 * @return array
	 */
	public function getBuffer()
	{
		return $this->_buffer;
	}
	
	/**
	 * Get Memory Usage
	 *
	 * @return float MB
	 */
	public function getMemUsage()
	{
		$mem = sprintf('%0.3f', memory_get_usage() / 1048576 );
		return $mem;
	}
	
	/**
	 * Output a time mark
	 *
	 * The mark is returned as text enclosed in <div> tags
	 * with a CSS class of 'profiler'.
	 *
	 * @access public
	 * @param string A label for the time mark
	 * @return string Mark enclosed in <div> tags
	 */
	public function mark($label, $package = 'system')
	{
		$mark = 'Package: <strong>' . $package . '</strong>. ';
		$mark .= $label .'. <strong>&raquo;</strong> ';
		$mark .= sprintf('Time %.5f', $this->_getMicrotime() - $this->_start) . ' seconds';
		$mark .= ', '.sprintf('%0.3f', memory_get_usage() / 1048576 ).' MB.';		
		$this->_buffer[] = $mark;
		return $mark;
	}
	
	/**
	 * Debug
	 * draw ra khối debug.
	 *
	 * @return HTML content
	 */
	public static function debug()
	{		
		if (!fConfig::get('debug')) {
			return;
		}
			
		$profiler = fProfiler::getInstance();
		ob_start();		
		echo '<style type="text/css">
	#system-debug{width: 100% !important; color:#555555; line-height: 1.5em;} 
	#system-debug div#sql-report strong{ color:#993333}; .blue {color:#00CC00;};
</style><div id="system-debug" class="clearfix"><h3>IN DEBUG ENVIROMENT</h3>';
		
		#List Mark
		echo '<h4>Activities Information</h4>';
		echo '<ol>';
		$marks = $profiler->getBuffer();		
		for ($i = 0, $msize = sizeof($marks); $i < $msize; ++$i)
		{
			echo '<li>' . $marks[$i] .'</li>';
		}
		echo '</ol>';		
		echo '<div>';
		echo '<h4>Memory Usage</h4>';
		$memoryLimit = (int) ini_get('memory_limit');
		$perMem = sprintf('%0.3f', ($profiler->getMemUsage() / $memoryLimit) * 100);
		if ($perMem >= 75) {
			$perMem = '<strong>' .$perMem .' %</strong>';			
		}
		else {
			$perMem = '<span class="blue">' . $perMem .'%</span>';
		}
		echo $profiler->getMemUsage() .' / ' . (int) ini_get('memory_limit') .' MB (' .$perMem  .')';
		echo '</div>';
		
        fDatabase::profiler();
			
		echo '<div><h4>Included files</h4><ol>';
		$files = get_included_files();
		for ($i = 0, $size = sizeof($files); $i < $size; ++$i)
		{
			echo '<li>' . $files[$i] . '</li>';
		}
		echo '</ol><br />Total <strong>' . $size . '</strong> included files.</div>';
		echo '</div>';
		
		$debug = ob_get_clean();
//		file_put_contents(_LOG_PATH_.'log'. time().'.txt', $debug);		
		return $debug;
	}
	
	/**
	 * Get the current time.
	 *
	 * @access public
	 * @return float The current time
	 */
	private function _getMicrotime()
	{
		list( $usec, $sec ) = explode( ' ', microtime() );
		return ((float)$usec + (float)$sec);
	}
	
	private function __clone() {}
}